package net.togogo.egouweb1238.core.controller.seller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import net.togogo.egouweb1238.common.page.Pagination;
import net.togogo.egouweb1238.core.domain.product.Brand;
import net.togogo.egouweb1238.core.domain.product.Color;
import net.togogo.egouweb1238.core.domain.product.Feature;
import net.togogo.egouweb1238.core.domain.product.Img;
import net.togogo.egouweb1238.core.domain.product.Product;
import net.togogo.egouweb1238.core.domain.product.Sku;
import net.togogo.egouweb1238.core.domain.product.Type;
import net.togogo.egouweb1238.core.query.product.BrandQuery;
import net.togogo.egouweb1238.core.query.product.ColorQuery;
import net.togogo.egouweb1238.core.query.product.FeatureQuery;
import net.togogo.egouweb1238.core.query.product.ProductQuery;
import net.togogo.egouweb1238.core.query.product.TypeQuery;
import net.togogo.egouweb1238.core.service.pagestatic.IPageStaticService;
import net.togogo.egouweb1238.core.service.product.IBrandService;
import net.togogo.egouweb1238.core.service.product.IColorService;
import net.togogo.egouweb1238.core.service.product.IFeatureService;
import net.togogo.egouweb1238.core.service.product.IProductService;
import net.togogo.egouweb1238.core.service.product.ISkuService;
import net.togogo.egouweb1238.core.service.product.ITypeService;

/**
 * 后台商品管理
 * 商品列表
 * 商品添加'
 * 商品上架
 * @author stone
 *
 */
@Controller
public class ProductController {
	@Resource
	private IBrandService brandService;
	@Resource
	private IProductService productService;
	@Resource
	private ITypeService typeService;
	@Resource
	private IFeatureService featureService;
	@Resource
	private IColorService colorService;

	@Resource(name="pageStaticService")
	private IPageStaticService pageStaticService;
	@Resource
	private ISkuService skuService;

	/**
	 * 显示商品列表信息
	 * @param session
	 * @param pageNo
	 * @param name
	 * @param brandId
	 * @param isShow
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/product/list.do")
	public String list(HttpSession session, Integer pageNo, String name, Integer brandId, Integer isShow,
			ModelMap model) {
		//品牌条件对象
		BrandQuery brandQuery = new BrandQuery();
		//设置指定字段
		brandQuery.setFields("id,name");
		//设置可见
		brandQuery.setIsDisplay(1);
		//加载品牌
		List<Brand> brands = brandService.getBrandList(brandQuery);
		//显示在页面
		model.addAttribute("brands", brands);

		//分页参数
		StringBuilder params = new StringBuilder();

		//商品条件对象
		ProductQuery productQuery = new ProductQuery();
		//1:判断条件是为Null
		if (StringUtils.isNotBlank(name)) {
			productQuery.setName(name);
			//要求模糊查询
			productQuery.setNameLike(true);

			params.append("&name=").append(name);

			//回显查询条件
			model.addAttribute("name", name);
		}
		//判断品牌ID
		if (null != brandId) {
			productQuery.setBrandId(brandId);
			params.append("&").append("brandId=").append(brandId);

			//回显查询条件
			model.addAttribute("brandId", brandId);
		}
		//判断上下架状态
		if (null != isShow) {
			productQuery.setIsShow(isShow);
			params.append("&").append("isShow=").append(isShow);

			//回显查询条件
			model.addAttribute("isShow", isShow);
		} else {
			productQuery.setIsShow(0);
			params.append("&").append("isShow=").append(0);
			//回显查询条件
			model.addAttribute("isShow", 0);
		}
		//设置页号
		productQuery.setPageNo(Pagination.cpn(pageNo));
		//设置每页数
		productQuery.setPageSize(5);
		//按照ID倒排
		productQuery.orderbyId(false);

		//加载带有分页的商品
		//list
		Pagination pagination = productService.getProductListWithPage(productQuery);

		//分页页面展示    //String params = "brandId=1&name=浪琴&pageNo=1";
		//商品
		//http://localhost:8080/product/list.do?brandId=1&name=浪琴&pageNo=1
		//品牌 /brand/list.do
		//http://localhost:8080/brand/list.do?brandId=1&name=浪琴&pageNo=1
		String url = "/product/list.do";
		pagination.pageView(url, params.toString());

		model.addAttribute("pagination", pagination);

		return "product/list";
	}

	//去添加页面
	@RequestMapping(value = "/product/toAdd.do")
	public String toAdd(ModelMap model) {
		//加载商品类型
		TypeQuery typeQuery = new TypeQuery();
		//指定查询哪些字段
		typeQuery.setFields("id,name");
		typeQuery.setIsDisplay(1);
		typeQuery.setParentId(0);
		List<Type> types = typeService.getTypeList(typeQuery);
		//显示在页面
		model.addAttribute("types", types); //request
		//加载商品品牌
		//品牌条件对象
		BrandQuery brandQuery = new BrandQuery();
		//设置指定字段
		brandQuery.setFields("id,name");
		//设置可见
		brandQuery.setIsDisplay(1);
		//加载品牌
		List<Brand> brands = brandService.getBrandList(brandQuery);
		//显示在页面
		model.addAttribute("brands", brands);
		//加载商品材质
		FeatureQuery featureQuery = new FeatureQuery();

		List<Feature> features = featureService.getFeatureList(featureQuery);
		//显示在页面
		model.addAttribute("features", features);
		//加载颜色
		ColorQuery colorQuery = new ColorQuery();
		colorQuery.setParentId(0);
		List<Color> colors = colorService.getColorList(colorQuery);
		//显示在页面
		model.addAttribute("colors", colors);

		return "product/add";
	}

	//商品添加
	@RequestMapping(value = "/product/add.do")
	public String add(Product product, Img img) {
		//1:商品 表   图片表   SKu表
		product.setImg(img);
		//传商品对象到Servcie
		productService.addProduct(product);

		return "redirect:/product/list.do";
	}

	/**
	 * 商品上架，实现商品详情页静态化
	 * @param ids
	 * @param pageNo
	 * @param name
	 * @param brandId
	 * @param isShow
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/product/isShow.do")
	public String isShow(Integer[] ids, Integer pageNo, String name, Integer brandId, Integer isShow, ModelMap model) {
		//实例化商品
		Product product = new Product();
		product.setIsShow(1);
		//上架
		if (null != ids && ids.length > 0) {
			for (Integer id : ids) {
				product.setId(id);
				//修改上架状态
				productService.updateProductById(product);
				//静态化 
				Map<String, Object> root = new HashMap<String, Object>();
				//设置值
				//商品加载
				Product p = productService.getProductById(id);

				root.put("product", p);

				//skus
				List<Sku> skus = skuService.getStock(id);
				root.put("skus", skus);
				//去重复
				List<Color> colors = new ArrayList<Color>();
				//遍历SKu
				for (Sku sku : skus) {
					//判断集合中是否已经有此颜色对象了
					if (!colors.contains(sku.getColor())) {
						colors.add(sku.getColor());
					}
				}
				root.put("colors", colors);
				pageStaticService.productDetail(root, id);
			}
		}

		//判断
		if (null != pageNo) {
			model.addAttribute("pageNo", pageNo);
		}
		if (StringUtils.isNotBlank(name)) {
			model.addAttribute("name", name);
		}
		if (null != brandId) {
			model.addAttribute("brandId", brandId);
		}
		if (null != isShow) {
			model.addAttribute("isShow", isShow);
		}

		return "redirect:/product/list.do";
	}
}
